|
|
|
|
|
|
|
The OpenService function is quite simple as well. Each of the parameters have been seen in the preceding declarations. The function declaration is shown here: |
|
|
|
|
|
|
|
|
Public Declare Function intOpenService Lib "advapi32" _
Alias "OpenServiceA" (ByVal hSCManager As Long, _
ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Long) As Long |
|
|
|
|
|
|
|
|
The ServiceManager object must be initialized after it is created to open the system's service manager. The OpenSCManager method corresponds to the Win32 API OpenSCManager function. The only interesting trick here is the conversion of the Machine and Database string variables to NULL strings from empty strings. If you leave it as an empty string, the API function will receive a pointer to a NULL character, rather than the required NULL value. |
|
|
|
|
|
|
|
|
The function stores the API handle in a private variable called schandle and returns the value. You aren't actually expected to do anything with the value; the fact that it is non-zero indicates that the function succeeded. This handle is closed during the object's Terminate event. The OpenSCManager method and terminate event follow: |
|
|
|
|
|
|
|
|
' Open the service control manager
' Returns non-zero on success
Public Function OpenSCManager(ByVal Machine As String, ByVal Database _
As String, rights As ServiceControlRights) As Long
' Close, then open if necessary
If schandle <> 0 Then Call CloseServiceHandle(schandle)
' Machine and Database must be valid or NULL, not empty
If Machine = "" Then Machine = vbNullString
If Database = "" Then Database = vbNullString
schandle = intOpenSCManager(Machine, Database, rights)
OpenSCManager = schandle
End Function
Private Sub Class_Terminate()
If schandle <> 0 Then Call CloseServiceHandle(schandle)
End Sub |
|
|
|
|
|
|
|
|
The OpenService function creates and initializes a ServiceObject object. The function first verifies that the ServiceManager object has been initialized by checking the value of the schandle variable. It then calls the OpenService API function to open the service. If the service is opened successfully, it calls the Initialize method of the ServiceObject object. This method is a Friend function. If |
|
|
|
|
|